﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NetDon.OA.Data.EntityClasses;
using NetDon.OA.Interface;
using NetDon.OA.Interface.eLogistics;
using NetDon.Utilities;

namespace NetDon.OA.Portal.Finance
{
    public partial class InvoiceInfo : UserControlBase
    {
        protected Guid Id
        {
            set { ViewState["Id"] = value; }
            get
            {
                if (ViewState["Id"] != null)
                    return ViewState["Id"].ToString().ToGuid();
                return Guid.Empty;
            }
        }
        protected Guid ClientId
        {
            set { ViewState["ClientId"] = value; }
            get
            {
                if (ViewState["ClientId"] != null)
                    return ViewState["ClientId"].ToString().ToGuid();
                return Guid.Empty;
            }
        }
        protected Guid RouteTypeId
        {
            set { ViewState["RouteTypeId"] = value; }
            get
            {
                if (ViewState["RouteTypeId"] != null)
                    return ViewState["RouteTypeId"].ToString().ToGuid();
                return Guid.Empty;
            }
        }

        protected IList<Guid> IssuingOrderIds
        {
            set { ViewState["IssuingOrderIds"] = value; }
            get
            {
                if (ViewState["IssuingOrderIds"] != null)
                    return ViewState["IssuingOrderIds"] as IList<Guid>;
                return new List<Guid>();
            }
        }

        public decimal? StateofAccountAmount
        {
            set { ViewState["StateofAccountAmount"] = value; }
            get
            {
                if (ViewState["StateofAccountAmount"] != null)
                    return ViewState["StateofAccountAmount"] as decimal?;
                return null;
            }
        }


        protected IList<Guid> RevenueMachineAccountIds
        {
            set { ViewState["RevenueMachineAccountIds"] = value; }
            get
            {
                if (ViewState["RevenueMachineAccountIds"] != null)
                    return ViewState["RevenueMachineAccountIds"] as IList<Guid>;
                return new List<Guid>();
            }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

            }
        }
        public void LoadInitialData(IList<Guid> issuingOrderId, IList<Guid> revenueMachineAccountIds, decimal? stateofAccountAmount)
        {
            IssuingOrderIds = issuingOrderId;
            RevenueMachineAccountIds = revenueMachineAccountIds;
            StateofAccountAmount = stateofAccountAmount;
            if (stateofAccountAmount.HasValue)
                this.txtStateofAccountAmount.Text = stateofAccountAmount.Value.ToString();
            this.txtConfirmInvoiceDate.Text =
    this.txtAccountsReceivableDate.Text = DateTime.Now.ToString("yyyy-MM-dd");


            var ordrerService = ServiceContainer.GetService<IIssuingOrderService>();
            var order = ordrerService.GetSimpleIssuingOrder(IssuingOrderIds.First());
            ClientId = order.CustomerId.Value;
            RouteTypeId = order.RouteTypeId;
            this.txtInvoiceAmount.Text = (stateofAccountAmount??0).ToString();
            this.lblRouteName.Text = this.Page.GetDictionaryItemName(order.RouteTypeId);
            this.txtCustomerName.Text = new CustomerInfoEntity(order.CustomerId.Value).CustomerName;

        }

        public bool LoadInitialData(Guid id)
        {
            Id = id;



            var service = ServiceContainer.GetService<IRevenueMachineAccountService>();
            var data = service.GetRevenueMachineAccountInvoice(Id);

            ClientId = data.CustomerId.Value;
            RouteTypeId = data.RouteTypeId ?? Guid.Empty;
            this.lblRouteName.Text = this.Page.GetDictionaryItemName(data.RouteTypeId);
            this.txtCustomerName.Text = new CustomerInfoEntity(data.CustomerId.Value).CustomerName;

            FormPanelInvoice.DataSource = data;
            FormPanelInvoice.DataBind();

            if (data.InvoiceCurrencyType.HasValue)
                this.ddlInvoiceCurrencyType.SelectedValue = data.InvoiceCurrencyType.Value.ToString();
            if (data.InvoiceType.HasValue)
                this.ddlInvoiceType.SelectedValue = data.InvoiceType.Value.ToString();

            if (IsAnyAccountReceipt(data) && !this.Page.IsManager())
            {
                FormPanelInvoice.Enabled = false;
                return false;
            }
            return true;

        }

        bool IsAnyAccountReceipt(RevenueMachineAccountInvoiceData data)
        {
            return (data.TotalAccountReceiptAmount.HasValue && data.TotalAccountReceiptAmount.Value > 0) ||
                   data.IsAccountReceiptCompleted;
        }

        public bool SaveInvoice()
        {

            var service = ServiceContainer.GetService<IRevenueMachineAccountService>();
            RevenueMachineAccountInvoiceData machineAccountData = null;
            if (Id == Guid.Empty)
            {
                machineAccountData = new RevenueMachineAccountInvoiceData();
            }
            else
            {
                machineAccountData = service.GetRevenueMachineAccountInvoice(Id);
            }
            var order = ExtractRevenueMachineAccountInvoiceData(machineAccountData);



            bool result = false;

            result = Id != Guid.Empty ? service.ModifyRevenueMachineAccountInvoice(order) : service.AddRevenueMachineAccountInvoice(order);
            return result;


        }

        private RevenueMachineAccountInvoiceData ExtractRevenueMachineAccountInvoiceData(RevenueMachineAccountInvoiceData machineAccountData)
        {

            machineAccountData.CustomerId = ClientId;
            machineAccountData.RouteTypeId = RouteTypeId;

            machineAccountData.ExpenseDescription = this.txtExpenseDescription.Text;
            machineAccountData.InvoiceNO = this.txtInvoiceNO.Text;
            machineAccountData.InvoiceType = this.ddlInvoiceType.SelectedValue.ToGuid();
            machineAccountData.InvoiceCurrencyType = this.ddlInvoiceCurrencyType.SelectedValue.ToGuid();
            machineAccountData.StateofAccountAmount = StateofAccountAmount;
            machineAccountData.InvoiceAmount = this.txtInvoiceAmount.Text.ToDecimal() ?? 0;
            machineAccountData.InvoiceDescription = this.txtInvoiceDescription.Text;

            machineAccountData.ConfirmInvoiceDate = DateTime.Parse(this.txtConfirmInvoiceDate.Text);
            machineAccountData.AccountsReceivableDate = DateTime.Parse(this.txtAccountsReceivableDate.Text);


            machineAccountData.LastUpdatedBy = CurrentApplicationContext.UserId;
            machineAccountData.LastUpdatedDate = DateTime.Now;
            machineAccountData.AreaId = CurrentApplicationContext.CompanyId;


            machineAccountData.RevenueMachineAccountInvoiceOrders = new List<RevenueMachineAccountInvoiceOrderData>();
            machineAccountData.RevenueMachineAccountInvoiceReceipts =
                new List<RevenueMachineAccountInvoiceReceiptData>();
            if (Id == Guid.Empty)
            {
                machineAccountData.CreateBy = CurrentApplicationContext.UserId;
                machineAccountData.CreatedDate = DateTime.Now;
                machineAccountData.Id = Guid.NewGuid();
                //extract RevenueMachineAccountInvoiceOrders
                foreach (var revenueMachineAccountId in RevenueMachineAccountIds)
                {
                    var revenueMachineAccountInvoiceOrder = new RevenueMachineAccountInvoiceOrderData();
                    revenueMachineAccountInvoiceOrder.LastUpdatedBy = CurrentApplicationContext.UserId;
                    revenueMachineAccountInvoiceOrder.LastUpdatedDate = DateTime.Now;
                    revenueMachineAccountInvoiceOrder.AreaId = CurrentApplicationContext.CompanyId;
                    revenueMachineAccountInvoiceOrder.CreateBy = CurrentApplicationContext.UserId;
                    revenueMachineAccountInvoiceOrder.CreatedDate = DateTime.Now;
                    revenueMachineAccountInvoiceOrder.RevenueMachineAccountId = revenueMachineAccountId;
                    revenueMachineAccountInvoiceOrder.Id = Guid.NewGuid();
                    machineAccountData.RevenueMachineAccountInvoiceOrders.Add(revenueMachineAccountInvoiceOrder);
                }
            }
            else
            {
                machineAccountData.Id = Id;
            }
            return machineAccountData;
        }


    }
}